package com.zhang.serviceprice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.dto.PriceRule;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.serviceprice.mapper.PriceRuleMapper;
import com.zhang.serviceprice.service.PriceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.scanner.Constant;

import java.util.HashMap;
import java.util.List;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-17
 */
@Service
public class PriceRuleServiceImpl implements PriceRuleService {
	@Autowired
	private PriceRuleMapper priceRuleMapper;

	@Override
	public ResponseResult add(PriceRule priceRule) {
		// 拼接fareType
		String cityCode = priceRule.getCityCode();
		String vehicleType = priceRule.getVehicleType();
		String fareType = cityCode + "$" + vehicleType;
		priceRule.setFareType(fareType);

		// 添加版本号
		HashMap<String, Object> map = new HashMap<>();
		map.put("cityCode", cityCode);
		map.put("vehicleType", vehicleType);

		QueryWrapper<PriceRule> wrapper = new QueryWrapper<>();
		wrapper.eq("city_code", cityCode);
		wrapper.eq("vehicle_type", vehicleType);
		wrapper.orderByDesc("fare_version");

		List<PriceRule> priceRules = priceRuleMapper.selectList(wrapper);
		Integer fareVersion = 0;
		if (priceRules.size() > 0) {
			return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(), CommonStatusEnum.PRICE_RULE_EXISTS.getValue());
		}
		priceRule.setFareVersion(++fareVersion);

		priceRuleMapper.insert(priceRule);
		return ResponseResult.success();
	}

	public ResponseResult edit(PriceRule priceRule) {
		// 拼接fareType
		String cityCode = priceRule.getCityCode();
		String vehicleType = priceRule.getVehicleType();
		String fareType = cityCode + "$" + vehicleType;
		priceRule.setFareType(fareType);

		// 添加版本号
		HashMap<String, Object> map = new HashMap<>();
		map.put("cityCode", cityCode);
		map.put("vehicleType", vehicleType);

		QueryWrapper<PriceRule> wrapper = new QueryWrapper<>();
		wrapper.eq("city_code", cityCode);
		wrapper.eq("vehicle_type", vehicleType);
		wrapper.orderByDesc("fare_version");

		List<PriceRule> priceRules = priceRuleMapper.selectList(wrapper);
		Integer fareVersion = 0;
		if (priceRules.size() > 0) {
			PriceRule lasterPriceRule = priceRules.get(0);
			Double unitPricePerMile = lasterPriceRule.getUnitPricePerMile();
			Double unitPricePerMinute = lasterPriceRule.getUnitPricePerMinute();
			Double startFare = lasterPriceRule.getStartFare();
			Integer startMile = lasterPriceRule.getStartMile();

			if (unitPricePerMile.doubleValue() == priceRule.getUnitPricePerMile().doubleValue()
					&& unitPricePerMinute.doubleValue() == priceRule.getUnitPricePerMinute().doubleValue()
					&& startFare.doubleValue() == priceRule.getStartFare().doubleValue()
					&& startMile.doubleValue() == priceRule.getStartMile().doubleValue()) {
				return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT.getCode(), CommonStatusEnum.PRICE_RULE_NOT_EDIT.getValue());
			}

			fareVersion = priceRules.get(0).getFareVersion();
		}
		priceRule.setFareVersion(++fareVersion);

		priceRuleMapper.insert(priceRule);
		return ResponseResult.success();
	}

	@Override
	public ResponseResult<PriceRule> getNewestVersion(String fareType) {
		QueryWrapper<PriceRule> wrapper = new QueryWrapper<>();
		wrapper.eq("fare_type", fareType);
		wrapper.orderByDesc("fare_version");
		List<PriceRule> priceRules = priceRuleMapper.selectList(wrapper);
		if (priceRules.size() > 0) {
			return ResponseResult.success(priceRules.get(0));
		} else {
			return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
		}
	}

	@Override
		public ResponseResult<Boolean> isNew(String fareType, Integer fareVersion) {
		ResponseResult<PriceRule> newestVersion = getNewestVersion(fareType);
		if (newestVersion.getCode() == CommonStatusEnum.PRICE_RULE_EMPTY.getCode()) {
			return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
		}

		PriceRule priceRule = newestVersion.getData();
		Integer fareVersionDB = priceRule.getFareVersion();
		if (fareVersionDB > fareVersion) {
			return ResponseResult.success(false);
		}else {
			return ResponseResult.success(true);
		}
	}

	@Override
	public ResponseResult<Boolean> ifExists(PriceRule priceRule) {
		String cityCode = priceRule.getCityCode();
		String vehicleType = priceRule.getVehicleType();

		QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("city_code", cityCode);
		queryWrapper.eq("vehicle_type", vehicleType);
		queryWrapper.orderByDesc("fare_version");
		List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
		if (!priceRules.isEmpty()) {
			return ResponseResult.success(true);
		}else {
			return ResponseResult.success(false);
		}
	}
}
